package com.zhaoyuxi.date;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "dlx.queue")
@Slf4j
public class ConsumerDTX {

    @RabbitHandler
    public void deleteOrder(String msg, Channel channel, Message message) throws IOException {
        try {
            log.info("下单超过20秒把订单状态值为无效状态开始...消息为：{}",msg);
            //此处省略100行代码  去修改订单状态为无效状态 执行一个update 语句 只有订单状态为未支付的时候才能修改成无效订单，否则不执行
            // update order set status = 9 where orderNo = 123546 and status = 0
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            if (message.getMessageProperties().getRedelivered()){
                log.info("消息归滚过一次,拒绝此消息......");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }else {
                log.info("消息重新回到队列中去执行......");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }

            e.printStackTrace();
        } finally {
        }

    }
}